<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
</head>
<body class="hold-transition skin-black sidebar-mini">
    <div th:replace="common/fragment :: link"></div>
    <div class="wrapper">
        <div th:replace="common/fragment :: navbar"></div>
        <div th:replace="common/fragment :: menu"></div>
        <div class="content-wrapper">
            <section class="content-header">
                <h1>角色编辑</h1>
            </section>
            <section class="content">
                <div class="box">
                    <form class="form-horizontal" action="/role/saveOrUpdate" method="post" id="editForm">
                        <input type="hidden" name="id" th:value="${role?.id}">
                        <div class="form-group"  style="margin-top: 10px;">
                            <label  class="col-sm-2 control-label">角色名称：</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" th:value="${role?.name}"  name="name" placeholder="请输入角色名称">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">角色编号：</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" th:value="${role?.sn}" name="sn" placeholder="请输入角色编号">
                            </div>
                        </div>
                        <div class="form-group " id="role">
                            <label for="role" class="col-sm-2 control-label">分配权限：</label><br/>
                            <div class="row" style="margin-top: 10px">

                                <!--左边权限框-->

                                <div class="col-sm-2 col-sm-offset-2">
                                    <select multiple class="form-control allPermissions" style="height: 350px;" size="15">
                                        <option th:each="permission,stat:${permissions}"
                                                th:value="${permission?.id}"
                                                th:text="${permission?.name}"></option>
                                    </select>
                                </div>
                                <div class="col-sm-1" style="margin-top: 60px;" align="center">
                                    <div>
                                        <a type="button" class="btn btn-primary" style="margin-top: 10px" title="右移动"
                                           onclick="moveSelected('allPermissions', 'selfPermissions')">
                                            <span class="glyphicon glyphicon-menu-right"></span>
                                        </a>
                                    </div>
                                    <div>
                                        <a type="button" class="btn btn-primary " style="margin-top: 10px" title="左移动"
                                           onclick="moveSelected('selfPermissions', 'allPermissions')">
                                            <span class="glyphicon glyphicon-menu-left"></span>
                                        </a>
                                    </div>
                                    <div>
                                        <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全右移动"
                                           onclick="moveAll('allPermissions', 'selfPermissions')">
                                            <span class="glyphicon glyphicon-forward"></span>
                                        </a>
                                    </div>
                                    <div>
                                        <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全左移动"
                                           onclick="moveAll('selfPermissions', 'allPermissions')">
                                            <span class="glyphicon glyphicon-backward"></span>
                                        </a>
                                    </div>
                                </div>

                                <!--右边权限框-->

                                <div class="col-sm-2">
                                    <select multiple class="form-control selfPermissions" style="height: 350px;" size="15" name="permissionIds">
                                        <option
                                                th:each="permission,stat:${ownPermissions}"
                                                th:value="${permission.id}"
                                                th:text="${permission.name}">
                                        </option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-1 col-sm-6">
                                <button type="button" class="btn btn-primary btn-submit">保存</button>
                                <a href="javascript:window.history.back()" class="btn btn-danger">取消</a>
                            </div>
                        </div>
                    </form>
                </div>
            </section>
        </div>
        <div th:replace="common/fragment :: footer"></div>
    </div>
</body>
<script>
    /*权限的左移右移功能*/
    function moveAll(srcClass, targetClass) {
        $('.' + targetClass).append($('.' + srcClass + "> option"));
    }
    function moveSelected(srcClass,targetClass) {
        $('.' + targetClass).append($('.'+srcClass + "> option:selected"))
    }

   /* 角色保存时全选右边权限下拉框的选项*/
    $(".btn-submit").click(function () {
        // 把右边下拉框中option全都选中
        $('.selfPermissions > option').prop('selected',true);
        // 发送请求
        $('#editForm').submit();
    });

    /*权限去重*/
    /*获取右边下拉框option value属性值*/

    // 角色去重
    // 获取右边下拉框中option value 的值
    var arr = [];
    $(".selfPermissions > option").each(function (i,doEle) {
        arr[i] = $(doEle).val();
    });
    console.log(arr);
// 遍历左边option，若其value 值存在上面数组中，则删除对应option 元素
    $(".allPermissions > option").each(function (i, domEle) {
        var $option = $(domEle);
        var val = $option.val();
        if ($.inArray(val, arr) >= 0) {
            $option.remove();
        }
    });


</script>
</html>